package com.qch.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException;

/**
 * @Author qch
 * @Date 2021/5/13
 */
public class HelloConsumeConsumer {
    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, TimeoutException {
        //获取连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //
        /**
         * 直接通过AMQP协议连接
         * 指定协议 amqp://
         * 指定密码 123456
         * 指定host 192.168.142.135
         * 指定端口 5672
         * 指定虚拟主机 %2f
         */
        factory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("queue.biz",false,false,true,null);
        /**
         * 监听消息，一旦有消息过来就调用第一个lamda表达式
         *
         */
        channel.basicConsume("queue.biz", (consumerTag, message) -> {
            System.out.println(new String(message.getBody()));
        }, (consumerTag) -> {});

        //channel.close();
        //connection.close();
    }
}
